#include "__PCH_PhysX.h"
#include "PhysicPrismaticJoint.h"

#include "NxPrismaticJointDesc.h"
#include "PhysicActor.h"
#include "Logger/Logger.h"

CPhysicPrismaticJoint::CPhysicPrismaticJoint()
: m_pJoint(NULL)
, m_pPrismaticJointDesc(NULL)
{
	m_pPrismaticJointDesc = new NxPrismaticJointDesc;
	
	m_pGlobalAnchor.zero();
	m_pGlobalAxis.zero();
}

CPhysicPrismaticJoint::~CPhysicPrismaticJoint()
{
	CHECKED_DELETE(m_pPrismaticJointDesc);
}

void CPhysicPrismaticJoint::CreateJoint(NxJoint* joint)
{
	assert(joint);
	m_pJoint = joint;

	CHECKED_DELETE(m_pPrismaticJointDesc);
}

void CPhysicPrismaticJoint::SetInfo	(const Vect3f& anchor, const Vect3f& axis, CPhysicActor* actorA,  CPhysicActor* actorB)
{
	this->m_pGlobalAnchor.x = anchor.x;
	this->m_pGlobalAnchor.y = anchor.y;
	this->m_pGlobalAnchor.z = anchor.z;

	this->m_pGlobalAxis.x = axis.x;
	this->m_pGlobalAxis.y = axis.y;
	this->m_pGlobalAxis.z = axis.z;

	if (actorA == NULL)
	{
		LOGGER->AddNewLog(ELL_ERROR, "PhysicSphericalJoint:: El primer actor pasado como argumento no puede ser null");
		return;
	}

	m_pPrismaticJointDesc->setToDefault();
	m_pPrismaticJointDesc->actor[0] = actorA->GetPhXActor();

	if (actorB != NULL)
	{
		m_pPrismaticJointDesc->actor[1] = actorB->GetPhXActor();
	}
	else
	{
		m_pPrismaticJointDesc->actor[1] = NULL;
	}

	m_pPrismaticJointDesc->setGlobalAnchor(m_pGlobalAnchor);
	m_pPrismaticJointDesc->setGlobalAxis(m_pGlobalAxis);
}

void CPhysicPrismaticJoint::SetLimitPlane(const float& DisplacementUnits)
{
	assert(m_pJoint != NULL);

	reinterpret_cast<NxPrismaticJoint*> (m_pJoint)->addLimitPlane(-m_pGlobalAxis, m_pGlobalAnchor + DisplacementUnits * m_pGlobalAxis);
	reinterpret_cast<NxPrismaticJoint*> (m_pJoint)->addLimitPlane(m_pGlobalAxis, m_pGlobalAnchor - DisplacementUnits * m_pGlobalAxis);
}